package com.mioto.ld.communication.mqtt.protocol.model;

import com.mioto.ld.communication.mqtt.protocol.*;
import com.mioto.ld.communication.mqtt.protocol.model.device.*;

import java.util.Arrays;

/**
 * @author mioto-qinxj
 * @date 2024/5/8
 * @desc 设备消息类型枚举
 */
public enum DeviceMTypeEnum {
     DEVICE_AUTH(200100,true, DeviceAuthPayload.class,new AuthHandleStrategy()),
     DEVICE_DATA_REPORT(200101,true, DeviceDataReportPayload.class,new ReportHandleStrategy()),
     DEVICE_FAULT_REPORT(200103,true, DeviceFaultReportPayload.class,new FaultHandleStrategy()),
     DEVICE_FAULT_RECOVERY_REPORT(200104,true, DeviceFaultReportPayload.class,new FaultRecoveryHandleStrategy()),
     DEVICE_PRESS_TIME_REPORT(200105,true, DevicePressTimePayload.class,new PressTimeHandleStrategy()),
     DEVICE_WILL(200102,false, Payload.class,new WillHandleStrategy()),
     DEVICE_REPLY(200300,false, DeviceReplyPayload.class,new ReplyHandleStrategy()),
     DEFAULT(000000,false,Payload.class,new DefaultHandleStrategy());

     public static Class<? extends Payload> getPayload(Integer mtype){
          return getInstance(mtype).payload;
     }

     public static DeviceMTypeEnum getInstance(Integer mtype){
          return Arrays.stream(DeviceMTypeEnum.values())
                  .filter(v -> v.mtype.equals(mtype))
                  .findFirst().orElse(DEFAULT);
     }

     /**
      * 消息类型
      */
     private Integer mtype;
     /**
      * 消息是否需要回复
      */
     private boolean reply;
     /**
      * 消息转换类
      */
     private Class<? extends Payload> payload;
     /**
      * 不同消息类别对应处理类
      */
     private IReceiptHandleStrategy handlerClazz;

     DeviceMTypeEnum(Integer mtype, boolean reply, Class<? extends Payload> payload, IReceiptHandleStrategy handlerClazz){
          this.payload = payload;
          this.reply = reply;
          this.mtype = mtype;
          this.handlerClazz =handlerClazz;
     }

     public Integer getMtype() {
          return mtype;
     }

     public boolean isReply() {
          return reply;
     }

     public IReceiptHandleStrategy getHandlerClazz() {
          return handlerClazz;
     }
}
